package com.prolestar.statistics.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.prolestar.publicsentiment.mapper.PsPublicsentimentMapper;
import com.prolestar.statistics.domain.BasicPublicsent;
import com.prolestar.statistics.req.StaticsIndexReq;
import com.prolestar.statistics.res.IndustryStatisticsRes;
import com.prolestar.statistics.res.PublicOpinionStatisticsRes;
import com.prolestar.statistics.res.StaticsIndexRes;
import com.prolestar.statistics.res.StatisticsOfPublicOpinion7DaysRes;
import com.prolestar.statistics.res.StatisticsOfPublicOpinionSourcesRes;
import com.prolestar.statistics.res.TerritorialStatisticsOfPublicOpinionRes;
import com.prolestar.statistics.res.YuqingDealData;
import com.prolestar.statistics.service.IStatisticsService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class StatisticsServiceImpl implements IStatisticsService {

	@Autowired
	private PsPublicsentimentMapper publicsentimentMapper;
	
	@Override
	public List<PublicOpinionStatisticsRes> publicOpinionStatistics() {
		log.info("舆情统计");
		List<PublicOpinionStatisticsRes> ress = new ArrayList<PublicOpinionStatisticsRes>();
		for (int i = 1; i <= 12; i++) {
			PublicOpinionStatisticsRes r = new PublicOpinionStatisticsRes();
			r.setMonth(i);
			ress.add(r);
		}
		List<BasicPublicsent> list =  publicsentimentMapper.selectByCreateTimeRange();
		for (BasicPublicsent b : list) {
			addCount(b,ress);
		}
		return ress;
	}

	private void addCount(BasicPublicsent b, List<PublicOpinionStatisticsRes> ress) {
		for (PublicOpinionStatisticsRes  r: ress) {
			if(r.getMonth() == b.month()) {
				r.add(b);
				break;
			}
		}
	}

	@Override
	public List<StatisticsOfPublicOpinionSourcesRes> statisticsOfPublicOpinionSources() {
		return null;
	}

	@Override
	public List<StatisticsOfPublicOpinion7DaysRes> statisticsOfPublicOpinion7Days() {



		List<StatisticsOfPublicOpinion7DaysRes> list = new ArrayList<StatisticsOfPublicOpinion7DaysRes>();
		Calendar cal = Calendar.getInstance();
		for (int i = 0; i < 7; i++) {
			//查询
			long count = publicsentimentMapper.selectCountByTime(cal.getTime());
			list.add(new StatisticsOfPublicOpinion7DaysRes(count, cal.getTime()));
			cal.add(Calendar.DATE, -1);
		}
		//查询数据
		return list;
	}

	@Override
	public List<TerritorialStatisticsOfPublicOpinionRes> territorialStatisticsOfPublicOpinion() {
		List<TerritorialStatisticsOfPublicOpinionRes> list = publicsentimentMapper.selectreleaseArea();
		return list;
	}

	@Override
	public List<IndustryStatisticsRes> industryStatistics() {
		List<IndustryStatisticsRes> list = publicsentimentMapper.selectIndustry();
		return list;
	}

	@Override
	public List<StaticsIndexRes> statics(StaticsIndexReq bean) {
		Date startTime = bean.getStartDate();
		Date endTime = bean.getEndDate();
		if(startTime == null) {
			startTime = new Date(0);
		}
		if(endTime == null) {
			endTime = new Date();
		}
		List<YuqingDealData> list = publicsentimentMapper.selectFindByTime(startTime, endTime);
		List<StaticsIndexRes> ress = new ArrayList<StaticsIndexRes>();
		for (YuqingDealData r : list) {
			StaticsIndexRes res = getRes(r,ress);
			res.add(r);
		}
		return ress;
	}

	private StaticsIndexRes getRes(YuqingDealData r, List<StaticsIndexRes> ress) {
		for (StaticsIndexRes res : ress) {
			if(res.getDepartId().equals(r.getDistriUnitId())) return res;
		}
		StaticsIndexRes res = new StaticsIndexRes();
		res.setDepartId(r.getDistriUnitId());
		res.setDepartName(r.getUnitName());
		ress.add(res);
		return res;
	}

}
